package com.saifan.wyy_ov.ui.communitycenter;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.data.bean.RentalBean;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.common.BaseFragment;
import com.saifan.wyy_ov.ui.communitylife.RentalinfoItemActivity;
import com.saifan.wyy_ov.ui.view.LabelView;
import com.saifan.wyy_ov.ui.view.RefreshLayout;
import com.saifan.wyy_ov.utils.CommonAdapter;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ToastUtil;
import com.saifan.wyy_ov.utils.ViewHolderHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @version V1.0.0
 * @author: lomasky
 * @date: 2015/6/9-21:19
 * @descrption 我的租售信息收藏
 */
public class RentalEnshrineFragment extends BaseFragment {
    private boolean isLoad;
    int page = 0;
    int type = 2;
    private RefreshLayout refreshLayout;
    private ListView listView;
    private List<RentalBean> beans = new ArrayList<>();
    private CommonAdapter<RentalBean> adapter;
    private View view;
    private GetData getData;
    private RelativeLayout loadlay;

    public RentalEnshrineFragment() {
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getData = new GetData();


    }

    public void query() {
        if (beans.isEmpty()) {
            loadlay.setVisibility(View.VISIBLE);
        }
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("UserID", getRoomInfoBean().getWYKHDA_ZJ());
            jsonObject.put("Page", page);
            jsonObject.put("Type", type);
            getData.getDataJson(getActivity(), Api.Collection.MY_COLLECTION, jsonObject, "", new ResponseData() {
                @Override
                public void onSuccess(String json) {
                    List<RentalBean> list = new Gson().fromJson(json, new TypeToken<List<RentalBean>>() {
                    }.getType());
                    if (!list.isEmpty()) {
                        if (page == 0) {
                            beans = list;
                        } else {
                            beans.addAll(list);
                        }
                    } else {
                        if (page < 1) {
                            beans.clear();
                            ToastUtil.showToast(getActivity(),"暂无相关信息");
                        }
                        page--;
                    }
                    adapter.mDatas = beans;
                    adapter.notifyDataSetChanged();
                    isLoad = true;
                    refreshLayout.setRefreshing(false);
                    refreshLayout.setLoading(false);
                    loadlay.setVisibility(View.GONE);

                }

                @Override
                public void onFailed(String str) {

                    refreshLayout.setRefreshing(false);
                    refreshLayout.setLoading(false);
                    loadlay.setVisibility(View.GONE);
                    if (page > 0) page--;

                }
            });
        } catch (JSONException e) {
        }
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if (!isLoad) {
                query();
            }
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_message, container, false);
        this.refreshLayout = (RefreshLayout) view.findViewById(R.id.refreshLayout);
        this.listView = (ListView) view.findViewById(R.id.listView);
        loadlay = (RelativeLayout) view.findViewById(R.id.loading_lay);
        initAdapter();
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // TODO Auto-generated method stub
                page = 0;
                refreshLayout.setRefreshing(true);
                query();
            }
        });

        refreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                // TODO Auto-generated method stub

                refreshLayout.setLoading(true);
                page++;
                query();
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), RentalinfoItemActivity.class);
                intent.putExtra("data", beans.get(position));
                startActivity(intent);


            }
        });
        return view;
    }

    public void initAdapter() {
        adapter = new CommonAdapter<RentalBean>(getActivity(), beans, R.layout.rentalinfo_item) {
            @Override
            public void convert(ViewHolderHelper helper, RentalBean item) {

                helper.setImageByUrl(R.id.CZCS_SLT, item.getCZCS_SLT());
                helper.setText(R.id.CZCS_JE, getString(R.string.rmb, String.valueOf(item.getCZCS_JE ())) );
                helper.setText(R.id.CZCS_BT, StringUtil.returnShow(item.getCZCS_BT()));
                helper.setText(R.id.CZCS_HXMC, StringUtil.returnShow(item.getCZCS_HXMC()));
                helper.setText(R.id.CZCS_CZLX, StringUtil.returnShow(item.getCZCS_CZLX()));
                if (!StringUtil.isNullOrEmpty(item.getCZCS_FBSJ()) && item.getCZCS_FBSJ().length() > 10) {
                    helper.setText(R.id.CZCS_FBSJ, StringUtil.returnShow(item.getCZCS_FBSJ().substring(0, 10)));
                }


                LabelView label = helper.getView(12345);
                if (label == null) {
                    label = new LabelView(getActivity());

                    label.setId(12345);

                }
                if (item.getCZCS_LX() == 1) {
                    label.setBackgroundColor(getResources().getColor(R.color.state3));
                    label.setTargetViewInBaseAdapter(helper.getView(R.id.CZCS_SLT), 92, 10, LabelView.Gravity.LEFT_TOP);
                    label.setText("出租");
                } else if (item.getCZCS_LX() == 2) {
                    label.setBackgroundColor(getResources().getColor(R.color.state1));
                    label.setTargetViewInBaseAdapter(helper.getView(R.id.CZCS_SLT), 92, 10, LabelView.Gravity.LEFT_TOP);
                    label.setText("出售");
                }


            }
        };

        listView.setAdapter(adapter);

    }

    public static RentalEnshrineFragment newInstance() {
        RentalEnshrineFragment f = new RentalEnshrineFragment();
        return f;
    }

}
